import gameCtr, { modelType } from "../ctrs/gameCtr";
import pvpCtr from "../ctrs/pvp/pvpCtr";

/**
 * @Author: 陈瑞鹏
 * @Date:   2022-11-26 10:02:16
 * @Last Modified by:   陈瑞鹏
 * @Last Modified time: 2023-01-05 15:58:56
 */
const { ccclass, property } = cc._decorator

@ccclass

export default class squareCom extends cc.Component {

  @property({ type: cc.Node, tooltip: "四个节点1" })
  pos1: cc.Node = null;

  @property({ type: cc.Node, tooltip: "四个节点2" })
  pos2: cc.Node = null;

  @property({ type: cc.Node, tooltip: "四个节点3" })
  pos3: cc.Node = null;

  @property({ type: cc.Node, tooltip: "四个节点4" })
  pos4: cc.Node = null;

  @property({ type: cc.Graphics })
  grap: cc.Graphics = null;

  cirCleColor = "#00FEEF"; //圆圈颜色

  grapColor = "#00BDFC"; //线条颜色

  protected onLoad(): void {

  }

  protected onEnable(): void {
    this.drawGrap();
  }

  public hp: number = 0;

  public circleId: number;

  public playerId: number;

  public isCanMove: boolean = true;

  public init(pos1, pos2, pos3, pos4) {
    this.pos1.setPosition(cc.v2(pos1.x, pos1.y));
    this.pos2.setPosition(cc.v2(pos2.x, pos2.y));
    this.pos3.setPosition(cc.v2(pos3.x, pos3.y));
    this.pos4.setPosition(cc.v2(pos4.x, pos4.y));
    this.pos1.color = new cc.Color().fromHEX(this.cirCleColor);
    this.pos2.color = new cc.Color().fromHEX(this.cirCleColor);
    this.pos3.color = new cc.Color().fromHEX(this.cirCleColor);
    this.pos4.color = new cc.Color().fromHEX(this.cirCleColor);
    if (gameCtr.ins.gameModel == modelType.pvp) {
      if (pvpCtr.ins && this.playerId == pvpCtr.ins.selfPlayerId) {
        this.pos1.on(cc.Node.EventType.TOUCH_START, this.onDown, this);
        this.pos2.on(cc.Node.EventType.TOUCH_START, this.onDown, this);
        this.pos3.on(cc.Node.EventType.TOUCH_START, this.onDown, this);
        this.pos4.on(cc.Node.EventType.TOUCH_START, this.onDown, this);
      } else {
        if (this.isCanMove) {
          this.pos1.on(cc.Node.EventType.TOUCH_START, this.onDown, this);
          this.pos2.on(cc.Node.EventType.TOUCH_START, this.onDown, this);
          this.pos3.on(cc.Node.EventType.TOUCH_START, this.onDown, this);
          this.pos4.on(cc.Node.EventType.TOUCH_START, this.onDown, this);
        }
      }

    } else {
      this.pos1.on(cc.Node.EventType.TOUCH_START, this.onDown, this);
      this.pos2.on(cc.Node.EventType.TOUCH_START, this.onDown, this);
      this.pos3.on(cc.Node.EventType.TOUCH_START, this.onDown, this);
      this.pos4.on(cc.Node.EventType.TOUCH_START, this.onDown, this);
    }

    this.drawGrap();
  }

  hurtBlood() {
    this.hp -= 1;
    if (this.hp <= 0) {
      this.recover();
    }
  }

  /**
   * 回收
   */
  recover() {
    this.pos1.setPosition(cc.v2(0, 0));
    this.pos2.setPosition(cc.v2(120, 0));
    this.pos3.setPosition(cc.v2(120, 120));
    this.pos4.setPosition(cc.v2(0, 120));

    this.pos1.off(cc.Node.EventType.TOUCH_START, this.onDown, this);
    this.pos2.off(cc.Node.EventType.TOUCH_START, this.onDown, this);
    this.pos3.off(cc.Node.EventType.TOUCH_START, this.onDown, this);
    this.pos4.off(cc.Node.EventType.TOUCH_START, this.onDown, this);
    this.cirCleColor = "#00FEEF"; //圆圈颜色

    this.grapColor = "#00BDFC"; //线条颜色

    gameCtr.ins.poolObj['square'].put(this.node);
  }

  public get pos1w() {
    return this.node.convertToWorldSpaceAR(this.pos1.getPosition());
  }

  public get pos2w() {
    return this.node.convertToWorldSpaceAR(this.pos2.getPosition());
  }

  public get pos3w() {
    return this.node.convertToWorldSpaceAR(this.pos3.getPosition());
  }

  public get pos4w() {
    return this.node.convertToWorldSpaceAR(this.pos4.getPosition());
  }

  /**
   * 点击
   */
  onDown(e) {
    e.currentTarget.on(cc.Node.EventType.TOUCH_MOVE, this.onMove, this);
    e.currentTarget.on(cc.Node.EventType.TOUCH_END, this.onUp, this);
    e.currentTarget.on(cc.Node.EventType.TOUCH_CANCEL, this.onUp, this);
    e.currentTarget.color = new cc.Color().fromHEX("#ffffff");
    gameCtr.ins.isMoveTriangle = true;
  }

  /**
   * 移动
   */
  onMove(e) {
    let nowPos = e.getLocation();
    let pos = this.node.convertToNodeSpaceAR(nowPos);
    e.currentTarget.setPosition(pos);

    if (gameCtr.ins.gameModel == modelType.pvp) {
      if (pvpCtr.ins) {
        pvpCtr.ins.sendClientInput({
          type: "smallCircleMove",
          // pos: cc.v2(0, 0),
          onePos: this.pos1.getPosition(),
          twoPos: this.pos2.getPosition(),
          threePos: this.pos3.getPosition(),
          fourPos: this.pos4.getPosition(),
          circleId: this.circleId,
          isMove: true,
        })
      } else {
        this.drawGrap();
      }

    } else {
      this.drawGrap();
    }
  }

  /**
   * 鼠标拿起
   * @param e 
   */
  onUp(e) {
    gameCtr.ins.isMoveTriangle = false;
    e.currentTarget.off(cc.Node.EventType.TOUCH_MOVE, this.onMove, this);
    e.currentTarget.off(cc.Node.EventType.TOUCH_END, this.onUp, this);
    e.currentTarget.off(cc.Node.EventType.TOUCH_CANCEL, this.onUp, this);
    e.currentTarget.color = new cc.Color().fromHEX(this.cirCleColor);
    if (gameCtr.ins.gameModel == modelType.pvp) {
      pvpCtr.ins.sendClientInput({
        type: "smallCircleMove",
        // pos: cc.v2(0, 0),
        onePos: this.pos1.getPosition(),
        twoPos: this.pos2.getPosition(),
        threePos: this.pos3.getPosition(),
        fourPos: this.pos4.getPosition(),
        circleId: this.circleId,
        isMove: false,
      })
    } else {
      this.drawGrap();
    }
  }

  /**
   * 绘制
   */
  drawGrap() {
    this.grap.clear();
    let pos1 = this.pos1.getPosition();
    this.grap.moveTo(pos1.x, pos1.y);
    let pos2 = this.pos2.getPosition();
    this.grap.lineTo(pos2.x, pos2.y);
    let pos3 = this.pos3.getPosition();
    this.grap.lineTo(pos3.x, pos3.y);
    let pos4 = this.pos4.getPosition();
    this.grap.lineTo(pos4.x, pos4.y);
    this.grap.close();

    this.grap.strokeColor = new cc.Color().fromHEX(this.grapColor);
    this.grap.stroke();
  }
}